package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.file.DatasourceManager;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import java.io.Serializable;

public class NameDatabaseConnection
  implements DatabaseConnection, Serializable
{
  private String name = null;
  private transient DatabaseConnection _database = null;

  public NameDatabaseConnection()
  {
  }

  public NameDatabaseConnection(String paramString)
  {
    this.name = paramString;
  }

  public String getName()
  {
    return this.name;
  }

  private DatabaseConnection createDatabase()
  {
    if (this._database == null)
    {
      DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
      Connection localConnection = localDatasourceManager.getConnection(getName());
      if (localConnection instanceof Serializable)
        this._database = ((Serializable)localConnection);
      else
        throw new RuntimeException(Inter.getLocText("Can_not_find_Data_Connection") + ":" + this.name);
    }
    return this._database;
  }

  public String getOriginalCharsetName()
  {
    return createDatabase().getOriginalCharsetName();
  }

  public void setOriginalCharsetName(String paramString)
  {
    createDatabase().setOriginalCharsetName(paramString);
  }

  public String getNewCharsetName()
  {
    return createDatabase().getNewCharsetName();
  }

  public void setNewCharsetName(String paramString)
  {
    createDatabase().setNewCharsetName(paramString);
  }

  public void testConnection()
    throws Exception
  {
    createDatabase().testConnection();
  }

  public java.sql.Connection createConnection()
    throws Exception
  {
    return createDatabase().createConnection();
  }

  public int hashCode()
  {
    if (this.name != null)
      return this.name.hashCode();
    return 0;
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof NameDatabaseConnection) && (ComparatorUtils.equals(getName(), ((NameDatabaseConnection)paramObject).getName())));
  }

  public String toString()
  {
    return "[" + getName() + " Database]";
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if ((paramXMLableReader.isChildNode()) && ("DatabaseName".equals(paramXMLableReader.getTagName())))
    {
      String str;
      if ((str = paramXMLableReader.getElementValue()) != null)
        this.name = str;
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (getName() != null)
      paramXMLPrintWriter.startTAG("DatabaseName").textNode(getName()).end();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }
}